var time = require("../../../../utils/util.js");
var app = getApp();
Page({
  data: {
    pageId: 0,
    message: [],
    messageGS: [],
    messageLB: [],
    txt: "",
    page: 1,
  },
  onLoad: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    this.setData({
      openid: openid
    })
    //发现产品
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/chanSearch',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        for (var i = 0; i < res.data.data.length; i++) {
          //时间
          var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
          res.data.data[i].createtime = time.timeHandle(d);
        }
        that.setData({
          message: res.data.data
        })
      }
    });
    //发现公司
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/gongSearch',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data.data.product) {
          //主营产品过长加省略号
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].product) {
              if (res.data.data[i].product.length > 24) {
                res.data.data[i].product = res.data.data[i].product.substring(0, 24) + '...'
              }
            }
          }
        }
        that.setData({
          messageGS: res.data.data
        })
      }
    });
    //发现老板
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/bosSearch',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data.data.product) {
          //主营产品过长加省略号
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].product) {
              if (res.data.data[i].product.length > 24) {
                res.data.data[i].product = res.data.data[i].product.substring(0, 24) + '...'
              }
            }
          }
        }
        that.setData({
          messageLB: res.data.data
        })
      }
    });
  },
  onShow: function() {

  },
  //返回发现
  catchHead: function() {
    wx.switchTab({
      url: '../faxian',
    })
  },
  //根据关键词搜索产品
  faxianChanPin: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/chanSearch',
      data: {
        openid: openid,
        title: e.detail.value
      },
      method: "post",
      success(res) {
        if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              }
            }
            //时间
            var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
          }
        }
        that.setData({
          message: res.data.data ? res.data.data : []
        })
      }
    })
  },
  //根据关键词搜索公司
  faxianGongSi: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/gongSearch',
      data: {
        openid: openid,
        name: e.detail.value
      },
      method: "post",
      success(res) {
        if (res.data.data) {
          //主营产品过长加省略号
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].product) {
              if (res.data.data[i].product.length > 24) {
                res.data.data[i].product = res.data.data[i].product.substring(0, 24) + '...'
              }
            }
          }
        }
        that.setData({
          messageGS: res.data.data ? res.data.data : []
        })
      }
    })
  },
  //根据关键词搜索老板
  faxianLaoBan: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/bosSearch',
      data: {
        openid: openid,
        name: e.detail.value
      },
      method: "post",
      success(res) {
        if (res.data.data) {
          //主营产品过长加省略号
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].product) {
              if (res.data.data[i].product.length > 24) {
                res.data.data[i].product = res.data.data[i].product.substring(0, 24) + '...'
              }
            }
          }
        }
        that.setData({
          messageLB: res.data.data ? res.data.data : []
        })
      }
    })
  },
  //上拉加载更多
  onReachBottom: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    this.setData({
      page: this.data.page + 1
    })
    //产品上拉
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/chanSearch',
      data: {
        openid: openid,
        page: this.data.page
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              } //时间
              var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
              res.data.data[i].createtime = time.timeHandle(d);
            }
            //添加到已有数组message
            that.data.message.push(res.data.data[i])
          }
          that.setData({
            message: that.data.message,
          })
        } else {
          wx.showToast({
            title: '暂无更多信息',
            icon: "none",
            duration: 1500,
            mask: true
          })
        }
      }
    });
    //公司上拉
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/gongSearch',
      data: {
        openid: openid,
        page: this.data.page
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.data) {
          //主营产品过长加省略号
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data.product) {
              if (res.data.data[i].product.length > 24) {
                res.data.data[i].product = res.data.data[i].product.substring(0, 24) + '...'
              }
            }
            //添加到已有数组messageGS
            that.data.messageGS.push(res.data.data[i])
          }
          that.setData({
            messageGS: that.data.messageGS
          })
        } else {
          wx.showToast({
            title: '暂无更多信息',
            icon: "none",
            duration: 1500,
            mask: true
          })
        }
      }
    });
    //老板上拉
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/bosSearch',
      data: {
        openid: openid,
        page: this.data.page
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.data) {
          //主营产品过长加省略号
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data.product) {
              if (res.data.data[i].product.length > 24) {
                res.data.data[i].product = res.data.data[i].product.substring(0, 24) + '...'
              }
            }
            //添加到已有数组messageGS
            that.data.messageLB.push(res.data.data[i])
          }
          that.setData({
            messageLB: that.data.messageLB
          })
        } else {
          wx.showToast({
            title: '暂无更多信息',
            icon: "none",
            duration: 1500,
            mask:true
          })
        }
      }
    });

  },
  //找产品
  goChanPin: function() {
    var pageId = 0;
    this.setData({
      pageId: pageId,
      txt: ""
    })
  },
  //找公司
  goGongSi: function() {
    var pageId = 1;
    this.setData({
      pageId: pageId,
      txt: ""
    })
  },
  //找老板
  goLaoBan: function() {
    var pageId = 2;
    this.setData({
      pageId: pageId,
      txt: ""
    })
  },
  //产品详情
  goSpecific: function(e) {
    wx.navigateTo({
      url: '../../jutixiangqing/chanpinjuti/chanpin?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //文章详情
  goWenZhang: function(e) {
    wx.navigateTo({
      url: '../../jutixiangqing/wenzhangjuti/wenzhang?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //名片
  goMingPian: function(e) {
    wx.navigateTo({
      url: '../../mingpian/mingpian?uid=' + e.currentTarget.dataset.uid,
    })
  },
  //加关注
  jiaguanzhu: function(e) {
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/focus',
      data: {
        openid: this.data.openid,
        aid: e.currentTarget.dataset.uid
      },
      method: "post",
      success(res) {
        console.log(res)
        for (var i = 0; i < that.data.messageGS.length; i++) {
          if (that.data.messageGS[i].uid == e.currentTarget.dataset.uid) {
            that.data.messageGS[i].fuid = 1;
          }
        }
        that.setData({
          messageGS: that.data.messageGS,
          messageLB: that.data.messageGS
        })
      }
    })
  },
  //取消关注
  quxiaoguanzhu: function(e) {
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/nofocus',
      data: {
        openid: this.data.openid,
        aid: e.currentTarget.dataset.uid
      },
      method: "post",
      success(res) {
        for (var i = 0; i < that.data.messageGS.length; i++) {
          if (that.data.messageGS[i].uid == e.currentTarget.dataset.uid) {
            that.data.messageGS[i].fuid = 0;
          }
        }
        that.setData({
          messageGS: that.data.messageGS,
          messageLB: that.data.messageGS
        })
      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function() {
    this.onLoad();
  }
})